#include <Editor/StdAfx.h>
#include "ParticleScriptPage.h"
#include "TextEditor.h"
#include "GameToolApp.h"
#include "EDITING_PARTICLE.h"
#include <boost/filesystem.hpp>

BEGIN_EVENT_TABLE(ParticleScriptPage, wxPanel)
	EVT_SIZE(OnSize)
END_EVENT_TABLE()

ParticleScriptPage::ParticleScriptPage(wxWindow* parent, int locale)
	: wxPanel(parent)
{
	mAuiMgr.SetManagedWindow(this);
	// FX Editor
	wxString strCandiFX = wxT("candidates_data_particle_object.txt");
	if (locale==wxLANGUAGE_KOREAN)
	{
		strCandiFX = wxT("candidates_data_particle_object_kr.txt");
	}
	mFXEditor = new TextEditor(this, strCandiFX);	

	wxSize clientSize = GetClientSize();
	wxAuiPaneInfo paneInfo;
	paneInfo.CaptionVisible(false);
	paneInfo.CloseButton(false);
	//paneInfo.MinSize((int)(clientSize.x), (int)(clientSize.y * 0.5f));
	//paneInfo.MinSize((int)(clientSize.x), (int)(clientSize.y * 0.5f));
	paneInfo.Caption(_("Particle Script Editor"));
	mAuiMgr.AddPane(mFXEditor, wxCENTER, _("Particle Script Editor"));
	//mFXEditor->SetFocus();
	
	
	// Material Editor
	wxString strCandiMaterial = wxT("candidates_data_material.txt");
	if (locale==wxLANGUAGE_KOREAN)
	{
		strCandiMaterial = wxT("candidates_data_material_kr.txt");
	}
	mMaterialEditor = new TextEditor(this, strCandiMaterial);
	paneInfo.Caption(_("Material Script Editor"));
	mAuiMgr.AddPane(mMaterialEditor, wxCENTER, _("Material Script Editor"));

	#ifdef _DEBUG
	const static wxString editingParticle = wxT("Data/particles/smoke2.particle");
	mFXEditor->LoadFile(editingParticle);
	mFXEditor->SetPath(editingParticle);
	//OnApplyParticleToView(wxCommandEvent());

	const static wxString editingMaterial = wxT("Data/materials/fx_smoke2.material");
	mMaterialEditor->LoadFile(editingMaterial);
	mMaterialEditor->SetPath(editingMaterial);
	#endif
	mAuiMgr.Update();
}


ParticleScriptPage::~ParticleScriptPage(void)
{
	mAuiMgr.UnInit();
}

void ParticleScriptPage::OnNewParticle(wxCommandEvent& e)
{
	if (mFXEditor->IsModifiedSinceLastSave())
	{
		wxMessageDialog confirm(this, _("Do you want to save the current editing particle?"), _("Save"), 
			wxYES_NO | wxCANCEL);
		int result = confirm.ShowModal();
		if (result == wxID_YES) {
			OnSaveParticle(e);
		} else if (result == wxID_CANCEL) {
			return;
		}
	}
	mFXEditor->SetPath(wxT(""));
	mFXEditor->SetText(wxT(""));

	wxGetApp().RemoveParticleSystem(EDITING_PARTICLE_NAME);

	mAuiMgr.GetPane(mFXEditor).caption = wxString(_T("Particle Script Editor")) + _T(" - ") + _T("NewParticle");
	mAuiMgr.Update();

}

bool ParticleScriptPage::OnLoadParticle(wxCommandEvent& e)
{
	if (mFXEditor->IsModifiedSinceLastSave())
	{
		wxMessageDialog confirm
			(this, _("Do you want to save the current editing particle?"), _("Save"), wxYES_NO | wxCANCEL);
		int result= confirm.ShowModal();
		if ( result== wxID_YES)	{
			OnSaveParticle(e);
		} else if (result == wxID_CANCEL) {
			return false;
		}
	}

	wxString strFileFilter = 
		_T("Ogre Particle Script(*.particle)|*.particle|\
		   All files(*.*)|*.*");

	wxString defaultDirectory = _T("Data/Particles/");
	if ( !wxDirExists(defaultDirectory) )
	{
		defaultDirectory.Clear();
	}
	std::auto_ptr<wxFileDialog> pOpenFileDialog
		( 
			new wxFileDialog
				( 
					this, _("Choose a particle script..."), defaultDirectory, _T(""),
					strFileFilter, 	wxFD_OPEN  | wxFD_FILE_MUST_EXIST
				)
		);
 
	wxString path;
	if ( pOpenFileDialog->ShowModal() == wxID_OK )
	{
		path = pOpenFileDialog->GetPath();
		mFXEditor->LoadFile(path);
		mFXEditor->SetPath(path);
		mAuiMgr.GetPane(mFXEditor).caption = 
		wxString(_T("Particle Script Editor")) + _T(" - ") + wxFileNameFromPath(path);

		/*
		wxString script = mFXEditor->GetText();
		unsigned pos = script.find( wxString("material", gCSConv) );
		unsigned start = pos+8;
		unsigned end = script.find(wxString("\n", gCSConv), start);
		wxString materialName = script.substr(start, end-start);
		const wxString szPath("data/materials/", gCSConv);
		materialName = materialName.Strip(wxString::both);
		wxString materialPath = szPath + materialName + wxString(".material", gCSConv);
		mMaterialEditor->LoadFile(materialPath);
		mMaterialEditor->SetPath(materialPath);*/

		mAuiMgr.Update();
		return true;
	}
	
	return false;
}

bool ParticleScriptPage::OnSaveParticle(wxCommandEvent& e)
{
	if (e.GetInt() == 0 && mFXEditor->GetTextLength() == 0)
		return false;

	wxString filepath = mFXEditor->GetPath();
	if (filepath.Len())
	{
		mFXEditor->SaveFile(filepath);
		mFXEditor->Saved();
		return true;
	}
	else
	{
		return OnSaveAsParticle(e);
	}
}

bool ParticleScriptPage::OnSaveAsParticle(wxCommandEvent& e)
{
	// save as flag. this forces saving even there is no change.
	e.SetInt(1);

	wxString strFileFilter = _( 
		"Ogre Particle Script(*.particle)|*.particle|\
		All files(*.*)|*.*");

	wxString currentFilepath = mFXEditor->GetPath();

	std::auto_ptr<wxFileDialog> pSaveFileDialog
		( 
			new wxFileDialog
				( 
					this, _("Save as..."), wxGetCwd(), currentFilepath, 
					strFileFilter, wxFD_SAVE | wxFD_OVERWRITE_PROMPT
				)
		);
 
	if ( pSaveFileDialog->ShowModal() == wxID_OK )
	{
		wxString path = pSaveFileDialog->GetPath();
		mFXEditor->SaveFile(path);
		mFXEditor->SetPath(path);
		mFXEditor->Saved();
		return true;
	}
	return false;
}

const wxString& ParticleScriptPage::GetParticlePath() const
{
	return mFXEditor->GetPath();
}

wxString ParticleScriptPage::GetParticleScript() const
{
	return mFXEditor->GetText();
}

void ParticleScriptPage::OnNewMaterial(wxCommandEvent& e)
{
	if (mMaterialEditor->IsModifiedSinceLastSave())
	{
		wxMessageDialog confirm(this, _("Do you want to save the current editing material?"), _("Save"), 
			wxYES_NO | wxCANCEL);
		int result = confirm.ShowModal();
		if ( result == wxID_YES) {
			OnSaveMaterial(e);
		} else if ( result == wxID_CANCEL) {
			return;
		}
	}
	mMaterialEditor->SetPath(_T(""));
	mMaterialEditor->SetText(_(""));
	

	mAuiMgr.GetPane(mMaterialEditor).caption = wxString(_T("Material Script Editor")) + _T(" - NewMaterial");
	mAuiMgr.Update();
}

bool ParticleScriptPage::OnLoadMaterial(wxCommandEvent& e)
{
	if (mMaterialEditor->IsModifiedSinceLastSave())
	{
		wxMessageDialog confirm
			(this, _("Do you want to save the current editing material?"), _("Save"), wxYES_NO | wxCANCEL);
		int result = confirm.ShowModal();
		if ( result == wxID_YES) {
			OnSaveMaterial(e);
		} else if (result == wxID_CANCEL) {
			return false;
		}
	}

	wxString strFileFilter = 
		_T( "Ogre Material Script(*.material)|*.material|\
			All files(*.*)|*.*"	);

	wxString defaultDirectory = _T("Data/Materials/");
	if (!wxDirExists(defaultDirectory.c_str()))
	{
		defaultDirectory.Clear();
	}
	std::auto_ptr<wxFileDialog> pOpenFileDialog
		( 
			new wxFileDialog
				( 
					this, _("Choose a particle script..."), defaultDirectory, _T(""), 
					strFileFilter, wxFD_OPEN  | wxFD_FILE_MUST_EXIST
				)
		);
 
	if ( pOpenFileDialog->ShowModal() == wxID_OK )
	{
		wxString path = pOpenFileDialog->GetPath();
		mMaterialEditor->LoadFile(path);
		mMaterialEditor->SetPath(path);
		mAuiMgr.GetPane(mMaterialEditor).caption = 
		wxString(_T("Material Script Editor")) + _T(" - ") + wxFileNameFromPath(pOpenFileDialog->GetPath());
		mAuiMgr.Update();
		return true;
	}
	return false;	
}

bool ParticleScriptPage::OnSaveMaterial(wxCommandEvent& e)
{
	if (e.GetInt() == 0 && mMaterialEditor->GetTextLength() == 0)
		return false;

	wxString filepath = mMaterialEditor->GetPath();
	if (filepath.Len())
	{
		mMaterialEditor->SaveFile(filepath);
		mMaterialEditor->Saved();
		return true;
	}
	else
	{
		return OnSaveAsMaterial(e);
	}
	
}

const wxString& ParticleScriptPage::GetMaterialPath() const
{
	return mMaterialEditor->GetPath();
}

wxString ParticleScriptPage::GetMaterialScript() const
{
	return mMaterialEditor->GetText();
}

bool ParticleScriptPage::OnSaveAsMaterial(wxCommandEvent& e)
{
	// save as flag. this forces saving even there is no change.
	e.SetInt(1);

	wxString strFileFilter = _T( 
		"Ogre Material Script(*.material)|*.material|\
		All files(*.*)|*.*");

	wxString currentFilepath = mMaterialEditor->GetPath();

	std::auto_ptr<wxFileDialog> pSaveFileDialog
		( 
			new wxFileDialog
				( 
					this, _("Save as..."), wxGetCwd(), currentFilepath, strFileFilter, 
					wxFD_SAVE | wxFD_OVERWRITE_PROMPT
				)
		);
 
	if ( pSaveFileDialog->ShowModal() == wxID_OK )
	{
		wxString path = pSaveFileDialog->GetPath();
		mMaterialEditor->SaveFile(path);
		mMaterialEditor->Saved();
		mMaterialEditor->SetPath(path);
		return true;
	}
	return false;

}

void ParticleScriptPage::CheckSave()
{
	if (mFXEditor->IsModifiedSinceLastSave())
	{
		wxMessageDialog confirm
			(this, _("Do you want to save the current editing particle?"), _("Save"), wxYES_NO);
		if (confirm.ShowModal() == wxID_YES)
		{
			OnSaveParticle(wxCommandEvent());
		}
	}

	if (mMaterialEditor->IsModifiedSinceLastSave())
	{
		wxMessageDialog confirm
			(this, _("Do you want to save the current editing material?"), _("Save"), wxYES_NO);
		if (confirm.ShowModal() == wxID_YES)
		{
			OnSaveMaterial(wxCommandEvent());
		}
	}
}

void ParticleScriptPage::UpdateAUI()
{
	mAuiMgr.Update();
}

void ParticleScriptPage::OnSize(wxSizeEvent& e)
{

}